var config = require('../../utils/config.js');
var netutil = require('../../utils/netutil.js');
var baseutil = require('../../utils/baseutil.js');
let { WeToast } = require('../../src/wetoast.js');
var register = require('../../utils/refreshLoadRegister.js');
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    TUserInfo: null,
    currentTab: 0,
    currentApprovalList: null,
    currtotal: 1,
    curr0: 1,
    total0: 1,
    approvalList0: new Array(),
    curr1: 1,
    total1: 1,
    approvalList1: new Array(),
    isShowAgreeDialog: false,//是否显示审批同意的dialog
    isShowRefuseDialog: false,//是否显示审批拒绝的dialog
    currentOperatePosition: -1,//当前操作的是第几条数据
    refuseReason: '',//拒绝原因385873
    isFirst: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    register.register(this);
    var that = this;       /**    * 获取系统信息    */
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth, winHeight: res.windowHeight
        });
      }
    });
    //创建可重复使用的toast实例，并附加到this上，通过this.wetoast访问
    new app.WeToast()

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      TUserInfo: app.globalData.TUserInfo,
      isLogin: app.globalData.isLogin
    })
    if (this.data.isLogin) {
      if (this.data.currentApprovalList==null){
        this.getApprovalOrderList()
      }
    } else {
      if (this.data.isFirst) {
        this.data.isFirst = false;
        this.wetoast.toast({
          title: '请先绑定!',
          duration: 500
        })
        setTimeout(function () {
          wx.navigateTo({ url: '/pages/bindphone/bindphone' })
        }, 500)
      }

    }
  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  updateData: function (TUserInfo) {
    baseutil.log("绑定成功")
    this.setData({
      TUserInfo: TUserInfo
    })
  },
  approvalDetail: function (e) {
    var position = e.currentTarget.dataset.position
    var ApprovalData = this.data.currentApprovalList[position]
    wx.navigateTo({
      url: '../approvalDetail/approvalDetail?OrderNo=' + ApprovalData.OrderNO + "&OrderType=" + ApprovalData.OrderType + "&isFrom=0"
    })
  },
  getApprovalOrderList: function () {
    var THIS = this
    var url = config.getBaseUrl("TravelApprove", "GetTravelApproveOrder");

    var Data = new Object();
    Data.SiteId = this.data.TUserInfo.SiteID
    Data.EID = this.data.TUserInfo.EID

    Data.CurrentUser = this.data.TUserInfo.UserID
    Data.PageSize = "10"
    Data.PageIndex = "1"
    var curr = 0;
    if (this.data.currentTab == 0) {
      Data.PageIndex = this.data.curr0;
      Data.CurrentApproveUser = this.data.TUserInfo.UserID
      Data.MyApproved = null
    } else if (this.data.currentTab == 1) {
      Data.PageIndex = this.data.curr1;
      Data.CurrentApproveUser = null
      Data.MyApproved = true
    }

    baseutil.log(url);
    netutil.post(
      url, Data,
      function (res) {
        baseutil.log(res.data);
        if (res.data.ResponseMetaInfo.StatusCode == '0') {

          if (THIS.data.currentTab == 0) {
            THIS.setData({
              total0: res.data.Data.Count,

            })
          } else if (THIS.data.currentTab == 1) {
            THIS.setData({
              total1: res.data.Data.Count,

            })
          }

          var approvalList = res.data.Data.List
          for (var i = 0; i < approvalList.length; i++) {
            var tempData = approvalList[i]
            var Description = tempData.TravelDescription.split(" ")
            tempData.Description = Description[0] + "(" + Description[3] + ")"
            tempData.time = Description[1] + " " + Description[2]
            approvalList[i] = tempData
          }

          if (approvalList.length > 0) {
            if (THIS.data.currentTab == 0) {
              if (THIS.data.curr0 == 1) {
                console.log("第一次添加")
                THIS.setData({
                  approvalList0: approvalList,
                  currentApprovalList: approvalList,
                  currtotal: approvalList.length
                })
              } else {
                console.log("第二次添加")
                var temp = THIS.data.approvalList0.concat(approvalList);
                THIS.setData({
                  approvalList0: temp,
                  currentApprovalList: temp,
                  currtotal: temp.length
                })
              }
            } else if (THIS.data.currentTab == 1) {
              if (THIS.data.curr1 == 1) {
                console.log("第一次添加")
                THIS.setData({
                  approvalList1: approvalList,
                  currentApprovalList: approvalList,
                  currtotal: approvalList.length
                })
              } else {
                console.log("第二次添加")
                var temp = THIS.data.approvalList1.concat(approvalList);
                THIS.setData({
                  approvalList1: temp,
                  currentApprovalList: temp,
                  currtotal: temp.length

                })
              }
            }
          } else {
            if (THIS.data.currentTab == 0 && THIS.data.curr0 == 1) {
              THIS.setData({
                currtotal: 0
              })
            } else if (THIS.data.currentTab == 1 && THIS.data.curr1 == 1) {
              THIS.setData({
                currtotal: 0
              })
            } else {
              THIS.setData({
                currtotal: THIS.data.currentApprovalList == null ? 0 : THIS.data.currentApprovalList.length
              })
            }


          }
          register.loadFinish(THIS, true);
        } else {
          THIS.wetoast.toast({
            title: res.data.ResponseMetaInfo.Reason,
            duration: 1000
          })

          register.loadFinish(THIS, true);
        }
      })
  },

  /**   * 点击tab切换   */
  swichNav: function (e) {

    if (this.data.currentTab == e.currentTarget.dataset.current) {
      return false;
    } else {

      this.setData({
        currentTab: e.currentTarget.dataset.current
      })
      console.log(this.data.currentTab + "=this.data.currentTab")
      if (this.data.currentTab == 0) {
        if (this.data.approvalList0 == null || this.data.approvalList0 == '' || this.data.approvalList0.length == 0) {
          this.refresh();
        }
        this.setData({
          currentApprovalList: this.data.approvalList0,
          currtotal: this.data.approvalList0.length
        })

      } else if (this.data.currentTab == 1) {
        if (this.data.approvalList1 == null || this.data.approvalList1 == '' || this.data.approvalList1.length == 0) {
          this.refresh();
        }
        this.setData({
          currentApprovalList: this.data.approvalList1,
          currtotal: this.data.approvalList1.length
        })

      }
    }

  },
  //模拟刷新数据
  refresh: function () {
    console.log("刷新数据!")

    if (this.data.currentTab == 0) {
      this.data.curr0 = 1;
    } else if (this.data.currentTab == 1) {
      this.data.curr1 = 1;
    }

    this.getApprovalOrderList();
  },
  //模拟加载更多数据
  loadMore: function () {
    if (this.data.currentTab == 0) {
      if (this.data.currtotal < this.data.total0) {
        this.data.curr0++;
        this.getApprovalOrderList();
      } else {
        register.loadFinish(this, true);
        this.wetoast.toast({
          title: '没有更多数据了',
          duration: 1000
        })
      }
    } else if (this.data.currentTab == 1) {
      if (this.data.currtotal < this.data.total1) {
        this.data.curr1++;
        this.getApprovalOrderList();
      } else {
        register.loadFinish(this, true);
        this.wetoast.toast({
          title: '没有更多数据了',
          duration: 1000
        })
      }
    }

  },
  agree: function (e) {
    var position = e.currentTarget.dataset.position
    baseutil.log(position)
    this.setData({
      isShowAgreeDialog: true,
      currentOperatePosition: position
    })
  },
  refuse: function (e) {
    var position = e.currentTarget.dataset.position
    baseutil.log(position)
    this.setData({
      isShowRefuseDialog: true,
      currentOperatePosition: position
    })
  },
  cancel: function (e) {
    var type = e.currentTarget.dataset.type
    if (type == 1) {
      this.setData({
        isShowAgreeDialog: false,
        currentOperatePosition: -1
      })
    } else {
      this.setData({
        isShowRefuseDialog: false,
        currentOperatePosition: -1
      })
    }
  },
  confirm: function (e) {
    var type = e.currentTarget.dataset.type
    var ApprovalData = this.data.currentApprovalList[this.data.currentOperatePosition]
    baseutil.log(ApprovalData)
    if (type == 1) {
      this.setData({
        isShowAgreeDialog: false,
        currentOperatePosition: -1
      })
    } else {
      this.setData({
        isShowRefuseDialog: false,
        currentOperatePosition: -1
      })
    }
    this.goOperate(ApprovalData, type);
  },
  goOperate(ApprovalData, type) {
    var THIS = this
    var url = config.getBaseUrl("TravelApprove", "DealApprove");
    var Data = new Object();
    Data.ApproveOrderID = ApprovalData.ApproveOrderID
    Data.CurrentApproveUser = app.globalData.TUserInfo.UserID
    Data.ApproveResult = type == 1 ? 1 : 2
    Data.Remark = this.data.refuseReason
    Data.ApproveType = 1

    baseutil.log(url);
    netutil.post(
      url, Data,
      function (res) {
        baseutil.log(res.data);
        if (res.data.ResponseMetaInfo.StatusCode == '0') {
          THIS.wetoast.toast({
            title: type == 1 ? '审批已同意!' : '审批已拒绝!',
            duration: 1000
          })
          THIS.refresh();
        } else {
          THIS.wetoast.toast({
            title: res.data.ResponseMetaInfo.Reason,
            duration: 1000
          })
        }
      })
  },
  getRefuseReason(e) {
    var refuseReason = e.detail.value;
    this.setData({ refuseReason: refuseReason })
  }
})